/**
* This is the item class for the Parallaxer library.
*
* @author Timothy Jordan, captaintimothy@gmail.com
*/
package {
  import flash.display.Sprite;
  import flash.display.Stage;
  import flash.geom.Rectangle;
  import flash.geom.Point;
  
  public class ParallaxerItem extends Sprite {
    private var _item:Sprite = null;
    public var depth:Number = 0;
    private var _adopt:Boolean = true;
    private var _bound:Object = null;
    private var _stage:Stage;
    
    public function ParallaxerItem(item:Sprite,
                                   depth:Number,
                                   stage:Stage,
                                   adopt:Boolean = true,
                                   bound:Object = null):void {
      _item = item;
      this.depth = depth;
      _stage = stage;
      _adopt = adopt;
      _bound = bound;
      
      if (_adopt) {
        addChild(_item);
      }
    }
    
    public function shift(dx:Number, dy:Number):Boolean {
      if (_adopt) {
        x += dx;
        y += dy;
      } else {
        _item.x += dx;
        _item.y += dy;
      }
      
      return true;
    }
    
    public function bounded(dx:Number, dy:Number):Boolean {
      if (_bound) {
        var rect:Rectangle;
        
        if (_adopt) {
          var point:Point = localToGlobal(new Point(x, y));
          rect = new Rectangle(point.x + dx, point.y + dy, width, height);
        } else {
          rect = new Rectangle(_item.x + dx, _item.y + dy, _item.width, _item.height);
        }
        
        if (_bound.left && rect.x > 0) {
          return false;
        }
        
        if (_bound.top && rect.y > 0) {
          return false;
        }
        
        if (_bound.right && rect.right < _stage.stageWidth) {
          return false;
        }
        
        if (_bound.bottom && rect.bottom < _stage.stageHeight) {
          return false;
        }
      }
      
      return true;
    }
  }
}